Passed
Pull Request — master (#112)
by Mathieu
01:49
created

DateUtilsAdapter.getWorkedFreeDays   A

Complexity

Conditions 1

Size

Total Lines 16
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 16
c 0
b 0
f 0
rs 9.6
cc 1
1
import {Injectable} from '@nestjs/common';
2
import {
3
  format as fnsFormat,
4
  isWeekend as fnsIsWeekend,
5
  getDaysInMonth as fnsGetDaysInMonth,
6
  eachDayOfInterval
7
} from 'date-fns';
8
import {IDateUtils} from 'src/Application/IDateUtils';
9
10
@Injectable()
11
export class DateUtilsAdapter implements IDateUtils {
12
  constructor(private readonly date: Date = new Date()) {}
13
14
  public format(date: Date, format: string): string {
15
    return fnsFormat(date, format);
16
  }
17
18
  public getDaysInMonth(date: Date): number {
19
    return fnsGetDaysInMonth(date);
20
  }
21
22
  public isWeekend(date: Date): boolean {
23
    return fnsIsWeekend(date);
24
  }
25
26
  public getCurrentDate(): Date {
27
    return this.date;
28
  }
29
30
  public getCurrentDateToISOString(): string {
31
    return this.date.toISOString();
32
  }
33
34
  public getWorkedDaysDuringAPeriod(start: Date, end: Date): Date[] {
35
    const dates: Date[] = [];
36
37
    for (const day of eachDayOfInterval({start, end})) {
38
      if (
39
        this.isWeekend(day) ||
40
        this.getWorkedFreeDays(day).filter(
41
          d => d.toISOString() === day.toISOString()
42
        ).length > 0
43
      ) {
44
        continue;
45
      }
46
47
      dates.push(day);
48
    }
49
50
    return dates;
51
  }
52
53
  public getWorkedFreeDays(date: Date): Date[] {
54
    const year = date.getFullYear();
55
56
    return [
57
      new Date(`${year}-01-01`),
58
      new Date(`${year}-04-21`),
59
      new Date(`${year}-05-01`),
60
      new Date(`${year}-05-08`),
61
      new Date(`${year}-05-29`),
62
      new Date(`${year}-06-09`),
63
      new Date(`${year}-07-14`),
64
      new Date(`${year}-08-15`),
65
      new Date(`${year}-11-01`),
66
      new Date(`${year}-11-11`),
67
      new Date(`${year}-12-25`)
68
    ];
69
  }
70
}
71